export const SelectDialogMixin = {
  props: {
    groupId: {
      type: String,
      required: true
    },
    initSelectedKeys: {
      type: Array,
      default: () => []
    },
    initSelectedRows: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      selectedRowKeys: [],
      selectionRows: [],
      visible: false,
      radio: false,
      dsCode: null,
      comp: ['comp', 'compCommon'],
      activeKey: 'comp'
    }
  },
  watch: {
    visible(value) {
      if (value) {
        this.selectedRowKeys = this.initSelectedKeys
        this.selectionRows = this.initSelectedRows
      }
    },
    initSelectedKeys: {
      handler(value) {
        this.selectedRowKeys = value
      }
    },
    initSelectedRows: {
      handler(value) {
        this.selectionRows = value
      }
    },
  },
  methods: {
    handleChange(activeKey) {
      this.activeKey = activeKey
    },
    handleCancel() {
      this.visible = false
      this.radio = false
      this.dsCode = null
      this.activeKey = this.comp[0]
    },
    setSelectedRowKeys(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    setSelectionRows(selectionRows) {
      this.selectionRows = selectionRows
    },
    handleOk() {
      if (this.radio) {
        if (this.selectedRowKeys.length === 0) {
          this.messageUtil.warning('请选择数据')
          return
        }
        if (this.selectedRowKeys.length > 1) {
          this.messageUtil.warning('存在多条数据')
          return
        }
        this.$emit('selectFinished', this.selectedRowKeys, this.selectionRows)
        this.handleCancel()
      } else {
        if (this.selectedRowKeys.length === 0) {
          this.messageUtil.warning('请选择数据')
          return
        }

        this.$emit('selectFinished', this.selectedRowKeys, this.selectionRows)
        this.handleCancel()
      }
    },
    handleClear() {
      this.$emit('selectFinished')
      this.handleCancel()
    }
  }
}
